//## 회계(입금) 저장 ####################
function fnSave()
{
	if (!$("#income").val())
	{
		alert("내역을 바르게 입력해 주세요!");
		$("#income").focus();
		return;
	}
	if (!$("#accountCd").val())
	{
		alert("계정을 선택해 해주세요!");
		$("#accountCd").focus();
		return;
	}
	if (!$("#incomeDt").val())
	{
		alert("입금일을 바르게 입력해 주세요!");
		$("#incomeDt").focus();
		return;
	}
	if (!$("#amount").val())
	{
		alert("금액을 바르게 입력해 주세요!");
		$("#amount").focus();
		return;
	}
	if (!$("#orgAmount").val())
	{
		alert("원금을 바르게 입력해 주세요!");
		$("#orgAmount").focus();
		return;
	}

	var valUrl = '/action/accountIn/save.asp';
	var param  = "income="+encodeURIComponent($("#income").val());
		param += "&memo="+encodeURIComponent($("#memo").val());
		param += "&incMemo="+encodeURIComponent($("#incMemo").val());
		param += "&incomeDt="+$("#incomeDt").val();
		param += "&accountCd="+$("#accountCd").val();
		param += "&amount="+$("#amount").val();
		param += "&orgAmount="+$("#orgAmount").val();
		param += "&tax="+$("#tax").val();
		param += "&incAmount="+$("#incAmount").val();
		param += "&actionDt="+$("#actionDt").val();
		param += "&bankDt="+$("#bankDt").val();
		param += "&incFlag="+$("#incFlag").val();
		param += "&incSeq="+$("#incSeq").val();
		param += "&compCd="+$("#compCd").val();
		param += "&seq="+$("#seq").val();
		param += "&flag="+$("#flag").val();
		param += "&billCd="+$("#billCd").val();
		param += "&part="+$("#part").val();
		param += "&billMemo="+encodeURIComponent($("#billMemo").val());

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			fnSessionCheck($(xml).find("MSG").text());
			if ($(xml).find("MSG").text() == "Save OK!")
			{
				alert("Save OK!");
				fnReset();
				grid01List();
			}
			else alert($(xml).find("MSG").text());
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}


//## 회계(입금) 그리드 표시 ####################
var G_curPage01 = 1;

function grid01List(page,accSum)
{	
	var gridNo = 1;
	var xmlNode = "IncomeList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage01;
	if (page == undefined) page = 1;
	else G_curPage01 = page;
	var limit = 50;

	var valUrl = "/action/accountIn/list.asp";

	var Parm = "findIncome="+encodeURIComponent($("#findIncome").val());
		Parm += "&findAccountCd="+$("#findAccountCd").val();
		Parm += "&findCompCd="+$("#findCompCd").val();
		Parm += "&findPart="+$("#findPart").val();
		Parm += "&findMnth="+$("#findMnth").val();
		Parm += "&keyset="+$("#keyset").val();
		Parm += "&stDate="+$("#stDate").val();
		Parm += "&endDate="+$("#endDate").val();
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",,,,left,,right,right,,right".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			if (ttlCnt%limit == 0) var totalPage = parseInt((ttlCnt / limit),0);
			else 	var totalPage = parseInt((ttlCnt / limit),0)  + 1;
			var cPageNo = (totalPage) * limit;
			if (cPageNo > ttlCnt) cPageNo = ttlCnt;
			
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var ttlAmt = 0;
			var ttlIncAmt = 0;
			var ttlOrgAmt = 0;
			var oldSeq = "";
			var empty = "&nbsp;";
			$(xml).find(xmlNode).each(function(idx){

				no = cPageNo;
				if (k>startNo && k<=endNo)
				{
					var seq				= $(this).find("seq").text();
					var incSeq				= $(this).find("incSeq").text();
					var accountNm		= $(this).find("accountNm").text();
					var income			= $(this).find("income").text();
					var incomeDt		= $(this).find("incomeDt").text();
					var bankDt		= $(this).find("bankDt").text();
					var partNm		= $(this).find("partNm").text();
					var actionDt		= $(this).find("actionDt").text();
					var incAmount		= $(this).find("incAmount").text();
					var company			= $(this).find("company").text();
					var amount			= $(this).find("amount").text();
					var orgAmount		= $(this).find("orgAmount").text();
					var restAmount		= $(this).find("restAmount").text();
					var tax				= $(this).find("tax").text();
					var memo			= $(this).find("memo").text();
					
					tax = "<br>"+tax;
					var style = "";
					if (seq == oldSeq) 
					{
						incomeDt = "-";
						partNm = "-";
						accountNm = "-";
						income = "-";
						company = "-";
						amount = "";
						orgAmount = "";
						tax = "";
					}
					var arrVal = new Array();
					arrVal[0] = "<p>"+no+"</p>";
					arrVal[1] = "<p>"+incomeDt.split("-").join(".")+"</p>";
					arrVal[2] = "<p>"+partNm+"</p>";
					arrVal[3] = "<p>"+accountNm+"</p>";
					arrVal[4] = "<p>"+income+"</p>";
					arrVal[5] = "<p>"+company+"&nbsp;"+"</p>";
					arrVal[6] = "<p>"+amount+"</p>";
					arrVal[7] = "<p style=\"margin-top:2px;\">"+orgAmount+tax+"</p>";
					arrVal[8] = "<p>"+actionDt.split("-").join(".")+"</p>";
					arrVal[9] = "<p>"+incAmount+"</p>";
					arrVal[10] = "<p>"+bankDt.split("-").join(".")+"</p>";
					
					if (amount!="") ttlAmt += parseInt(amount.split(",").join(""),10);
					if (incAmount!=0) ttlIncAmt += parseInt(incAmount.split(",").join(""),10);
					if (orgAmount!=0) ttlOrgAmt += parseInt(orgAmount.split(",").join(""),10);
					
					if (restAmount == 0) bgColor="#F2F2F2";
					else bgColor="#FFF";
										
					var link = "fnSetIncome('"+seq+"','" +incSeq+"')";

					//gridList = fnDrawCell(lotYear, lotNo, lotSubNo, processCd);
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				cPageNo--;
				oldSeq = seq;
				k++;			
			});
			var arrVal = new Array();
			arrVal[0] = "<p>"+empty+"</p>";
			arrVal[1] = "<p><b>합 계</b></p>";
			arrVal[2] = "<p>"+empty+"</p>";
			arrVal[3] = "<p>"+empty+"</p>";
			arrVal[4] = "<p>"+empty+"</p>";
			arrVal[5] = "<p>"+empty+"&nbsp;"+"</p>";
			arrVal[6] = "<p><b>"+getNumWithComma(ttlAmt)+"</b></p>";
			arrVal[7] = "<p><b>"+getNumWithComma(ttlOrgAmt)+"</b></p>";
			arrVal[8] = "<p>"+empty+"</p>";
			arrVal[9] = "<p><b>"+getNumWithComma(ttlIncAmt)+"</b></p>";
			arrVal[10] = "<p>"+empty+"</p>";
			bgColor="#f0faff";
			gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', '', cellLink);
			$("#"+"gridBody"+gridNo).prepend(gridList);
			$("#"+"gridBody"+gridNo).append(gridList);
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});
	if (accSum == undefined) setTimeout("grid03List(1);",500);
}

//## 회계(입금) 계정별 합계 그리드 표시 ####################
var G_curPage02 = 1;

function grid02List(page)
{	
	$("#layerSum1").css("display","none");
	$("#layerSum2").css("display","");
	
	var gridNo = 2;
	var xmlNode = "IncomeSumList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage02;
	if (page == undefined) page = 1;
	else G_curPage02 = page;
	var limit = 50;

	var valUrl = "/action/accountIn/listAccountSum.asp";

	var Parm = "findIncome="+encodeURIComponent($("#findIncome").val());
		Parm += "&findAccountCd="+$("#findAccountCd").val();
		Parm += "&findCompCd="+$("#findCompCd").val();
		Parm += "&findPart="+$("#findPart").val();
		Parm += "&findMnth="+$("#findMnth").val();
		Parm += "&keyset="+$("#keyset").val();
		Parm += "&stDate="+$("#stDate").val();
		Parm += "&endDate="+$("#endDate").val();
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",,right,right,right".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var empty = "&nbsp;";
			var ttlAmt = 0;
			var ttlIncAmt = 0;
			$(xml).find(xmlNode).each(function(idx){

				if (k>startNo && k<=endNo)
				{
					var accountNm		= $(this).find("accountNm").text();
					var accountCd		= $(this).find("accountCd").text();
					var amount			= $(this).find("amount").text();
					var incAmt			= $(this).find("incAmt").text();
					var orgAmt		= $(this).find("orgAmt").text();
					var taxAmt			= $(this).find("taxAmt").text();
					var notInAmt			= $(this).find("notInAmt").text();
					if (accountNm.indexOf("TOTAL")>0) { no = "&nbsp;"; bgColor="#f0faff"; }
					
					ttlAmt = amount;
					ttlIncAmt = incAmt;
					
					if (no > 1) 	var vNo = no -1;
					else var vNo = "&nbsp;";
					var arrVal = new Array();
					arrVal[0] = "<p>"+vNo+"</p>";
					arrVal[1] = "<p>"+accountNm+"</p>";
					//arrVal[2] = "<p>"+amount+"</p>";
					if (accountNm.indexOf("TOTAL")>0) arrVal[2] = "<p style=\"margin-top:2px;\">"+amount+"<br>"+orgAmt+"</p>";
					else  arrVal[2] = "<p>"+amount+"</p>";
					arrVal[3] = "<p>"+incAmt+"</p>";
					arrVal[4] = "<p style='color#:ff0000;'>"+notInAmt+"</p>";
															
					var link = "fnAccList('"+accountCd+"')";
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				no = parseInt(endNo,10)-parseInt(k,10);
				k++;			
			});
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});

}

//## 회계(입금) 회사별 합계 그리드 표시 ####################
var G_curPage03 = 1;

function grid03List(page)
{	
	$("#layerSum1").css("display","");
	$("#layerSum2").css("display","none");
	
	var gridNo = 3;
	var xmlNode = "IncomeSumList";

	fnResetGrid(gridNo);
	fnNewLoading(gridNo);

	if (!page) page = G_curPage03;
	if (page == undefined) page = 1;
	else G_curPage03 = page;
	var limit = 100;

	var valUrl = "/action/accountIn/listCompanySum.asp";

	var Parm = "findIncome="+encodeURIComponent($("#findIncome").val());
		Parm += "&findAccountCd="+$("#findAccountCd").val();
		Parm += "&findCompCd="+$("#findCompCd").val();
		Parm += "&findPart="+$("#findPart").val();
		Parm += "&findMnth="+$("#findMnth").val();
		Parm += "&keyset="+$("#keyset").val();
		Parm += "&stDate="+$("#stDate").val();
		Parm += "&endDate="+$("#endDate").val();
	if (xMode=="Debug") window.open(valUrl+'?xMode=debug&'+Parm);

	// 그리드 헤더를 기준으로 Cell의 폭을 구해온다.
	var cellWidth = fnGetGridCellWidth(gridNo);	
	// Cell의 정렬방식
	var cellAlign = ",left,right,right,right".split(",");
	// Cell의 링크 걸지 여부 결정
	var cellLink = ",,,,,,".split(",");

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: Parm,
		success: function(xml){
			var gridList = "";
			var k = 1;
			var ttlCnt = $(xml).find(xmlNode).length;
			var startNo = (parseInt(page,10)-1) * limit;
			var endNo = parseInt(page,10) * limit;
			var bgColor = "#FFF";
			var chkCnt = 0;
			
			// 그리드의 부가 정보(레코드수, 페이징) 표시 또는 가리기
			fnGridExtraInfo(gridNo, ttlCnt, page, limit); 

			if (ttlCnt <= endNo) endNo = ttlCnt;
			var no = endNo;
			var empty = "&nbsp;";
			var ttlAmt = 0;
			var ttlIncAmt = 0;
			$(xml).find(xmlNode).each(function(idx){

				if (k>startNo && k<=endNo)
				{
					var company		= $(this).find("company").text();
					var compCd		= $(this).find("compCd").text();
					var amount			= $(this).find("amount").text();
					var incAmt			= $(this).find("incAmt").text();
					var orgAmt		= $(this).find("orgAmt").text();
					var taxAmt			= $(this).find("taxAmt").text();
					var notInAmt			= $(this).find("notInAmt").text();
					if (company.indexOf("TOTAL")>0) { no = "&nbsp;"; bgColor="#f0faff"; }
					
					ttlAmt = amount;
					ttlIncAmt = incAmt;
					
					if (no > 1) 	var vNo = no -1;
					else var vNo = "&nbsp;";
					var arrVal = new Array();
					arrVal[0] = "<p>"+vNo+"</p>";
					arrVal[1] = "<p>"+company+"</p>";
					if (company.indexOf("TOTAL")>0) arrVal[2] = "<p style=\"margin-top:2px;\">"+amount+"<br>"+orgAmt+"</p>";
					else  arrVal[2] = "<p>"+amount+"</p>";
					arrVal[3] = "<p>"+incAmt+"</p>";
					arrVal[4] = "<p style='color#:ff0000;'>"+notInAmt+"</p>";

					var link = "fnCompList('"+company+"','"+compCd+"')";
					gridList = fnDrawCell(arrVal, bgColor, cellWidth, cellAlign, '', link, cellLink);

					$("#"+"gridBody"+gridNo).append(gridList);
				}
				no = parseInt(endNo,10)-parseInt(k,10);
				k++;			
			});
		},
		error: function(xml){
			alert('Error!!\r\n'+xml.responseText);
		}
	});

}

//## 회사별 합계 검색
function fnCompList(company, compCd)
{
	fnSearchReset();
	fnFindCompany(company,compCd,'findCompCd');
	setTimeout("grid01List(1,'Company');",500);
}

//## 계정별 합계 검색
function fnAccList(accountCd)
{
	fnSearchReset();
	$("#findAccountCd").val(accountCd);
	grid01List(1,'Account');
}

//## 회계(입금) 표시 ####################
function fnSetIncome(seq, incSeq)
{
	var valUrl = "/action/accountIn/view.asp";
	var param = "seq="+seq;
		param += "&incSeq="+incSeq;

	if (xMode=="debug") window.open(valUrl+'?xMode=debug&'+param);

	$.ajax({
		url: valUrl,
		type: 'POST',
		dataType: 'xml',
		data: param,
		success: function(xml){
			
			//## 로그인 체크
			fnSessionCheck($(xml).find("MSG").text());
			fnReset();

			$("#seq").val($(xml).find("seq").text());
			$("#incSeq").val($(xml).find("incSeq").text());
			$("#incAmount").val($(xml).find("incAmount").text());
			$("#actionDt").val($(xml).find("actionDt").text());
			$("#bankDt").val($(xml).find("bankDt").text());
			$("#incMemo").val($(xml).find("incMemo").text());
			$("#incFlag").val($(xml).find("incFlag").text());
			$("#accountCd").val($(xml).find("accountCd").text());
			$("#income").val($(xml).find("income").text());
			$("#compCd").val($(xml).find("compCd").text());
			$("#amount").val($(xml).find("amount").text());
			$("#orgAmount").val($(xml).find("orgAmount").text());
			$("#tax").val($(xml).find("tax").text());
			$("#incomeDt").val($(xml).find("incomeDt").text());
			$("#memo").val($(xml).find("memo").text());
			$("#part").val($(xml).find("part").text());
			$("#billCd").val($(xml).find("billCd").text());
			$("#billMemo").val($(xml).find("billMemo").text());
			$("#accessInfo").val("최종수정 : "+$(xml).find("modDt").text());
			fnFindCompany('',$(xml).find("compCd").text(),'compCd');
			
		},
		error: function(xml){
			alert('Error!!');
			alert(xml.responseText);
		}
	});
}

function fnSetSame()
{
	$("#incAmount").val($("#amount").val());
}

//## 지급일 동기화
function fnSetSameDt()
{
	$("#bankDt").val($("#incomeDt").val());
	$("#actionDt").val($("#incomeDt").val());
}

//## 회계(입금) Reset 기능
function fnReset()
{
	$("#income").val("");
	$("#memo").val("");
	$("#billMemo").val("");
	$("#billCd").val("");
	$("#accountCd").val("");
	$("#amount").val("");
	$("#orgAmount").val("");
	$("#tax").val("");
	$("#part").val("");
	$("#compCd").val("");
	$("#company").val("");
	$("#seq").val("");
	$("#flag").val("Y");
	$("#incAmount").val("");
	$("#incSeq").val("");
	$("#bankDt").val("");
	$("#actionDt").val("");
	$("#incFlag").val("Y");
}

function fnResetTerms(f)
{
	if (f=="t") 
	{
		$("#stDate").val("");
		$("#endDate").val("");		
	}
	else $("#findMnth").val("");		
}

function fnIncResultReset()
{
	$("#incSeq").val("");
	$("#incAmount").val("");
	$("#incMemo").val("");
	$("#incFlag").val("Y");
}

function fnSetTax()
{
	var amount = $("#amount").val();
	amount = amount.split(",").join("");
	if (amount > 0)
	{
		var tax = 0;
		tax = Math.round(parseInt(amount,10)/11);

		var orgAmount = parseInt(amount,10) - parseInt(tax,10);
		orgAmount = getNumWithComma(orgAmount);
		tax = getNumWithComma(tax);

		$("#tax").val(tax);
		$("#orgAmount").val(orgAmount);
	}
}

//## 검색 Reset 기능
function fnSearchReset()
{
	$("#findIncome").val("");
	$("#findAccountCd").val("");
	$("#findComapny").val("");
	$("#findCompCd").val("");
	$("#findPart").val("");
}